/**
 * 
 */
package com.angis.fx.activity.search;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.angis.fx.activity.R;
import com.angis.fx.activity.map.SearchInMapActivity;
import com.angis.fx.activity.service.CsCoordUploadService;
import com.angis.fx.activity.service.GPSService;
import com.angis.fx.data.ChangsuoInformation;
import com.angis.fx.util.GeoLatLng;

/**
 * @author Han
 *
 */
public class ChangsuoPositionActivity extends Activity {
	
	private ChangsuoInformation mChangsuoInfo;
	
	private TextView mCsLonText;
	
	private TextView mCsLatText;
	
	private Button mShowCSPostion;
	
	private Button mGetCurrentPostion;
	
	private Button mUpLoadPosition;
	
	private Button mShowCurPosition;
	
	private EditText mCurrentLon;
	
	private EditText mCurrentLat;
	
	private double l_lon;
	
	private double l_lat;
	
    private UpdateReceiver receiver;
    
    private IntentFilter filter;
    
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.changsuoposition);
		mChangsuoInfo = (ChangsuoInformation)getIntent().getExtras().get("selectedcs");
		initActivity();
		setValue();
	}

	private void setValue() {
		mCsLonText.setText(String.valueOf(mChangsuoInfo.getLon()));
		mCsLatText.setText(String.valueOf(mChangsuoInfo.getLat()));
	}

	private void initActivity() {
		mCsLonText = (TextView) findViewById(R.id.cspositionlon);
		mCsLatText = (TextView) findViewById(R.id.cspositionlat);
		mShowCSPostion = (Button) findViewById(R.id.getplacepositionbutton);
		mCurrentLon = (EditText) findViewById(R.id.currentpositionlon);
		mCurrentLat = (EditText) findViewById(R.id.currentpositionlat);
		mGetCurrentPostion = (Button)findViewById(R.id.getcurrentpostionbutton);
		mUpLoadPosition = (Button) findViewById(R.id.updatepostionbutton);
		mShowCurPosition = (Button) findViewById(R.id.showcurrentpostionbutton);
		mShowCurPosition.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View v) {
				try{
					if (mCurrentLon.getText()!=null && !mCurrentLon.getText().toString().trim().equals("")
							&& mCurrentLat.getText()!=null && !mCurrentLat.getText().toString().trim().equals("")){
						try{
							ChangsuoPositionActivity.this.unregisterReceiver(receiver);
						}catch (Exception e){
							
						}
						Intent intent = new Intent(ChangsuoPositionActivity.this,GPSService.class); 
						stopService(intent);
						mChangsuoInfo.setXlonGPS(Double.parseDouble(mCurrentLon.getText().toString()));
						mChangsuoInfo.setYlatGPS(Double.parseDouble(mCurrentLat.getText().toString()));
						Intent lIntent = new Intent();
						lIntent.putExtra("selectedcs", mChangsuoInfo);
						lIntent.setClass(ChangsuoPositionActivity.this,
								SearchInMapActivity.class);
						ChangsuoPositionActivity.this
								.startActivity(lIntent);
					} else {
						Toast.makeText(ChangsuoPositionActivity.this, "尚未获得GPS坐标", Toast.LENGTH_SHORT).show();
					}
				} catch(Exception e){
					Toast.makeText(ChangsuoPositionActivity.this, "无法获取GPS信息", Toast.LENGTH_SHORT).show();
				}
			}
		});
		
		mUpLoadPosition.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View v) {
				try{
					if (mCurrentLon.getText()!=null && !mCurrentLon.getText().toString().trim().equals("")
							&& mCurrentLat.getText()!=null && !mCurrentLat.getText().toString().trim().equals("")){
//						ChangsuoPositionActivity.this.registerReceiver(receiver, filter);
				        Intent intent = new Intent(ChangsuoPositionActivity.this,CsCoordUploadService.class);  
				        intent.putExtra("csinfo", mChangsuoInfo);
				        startService(intent); 
				        Toast.makeText(ChangsuoPositionActivity.this, "正在更新GPS信息", Toast.LENGTH_SHORT).show();
					} else {
						Toast.makeText(ChangsuoPositionActivity.this, "尚未获得GPS坐标", Toast.LENGTH_SHORT).show();
					}
				} catch(Exception e){
					Toast.makeText(ChangsuoPositionActivity.this, "无法获取GPS信息", Toast.LENGTH_SHORT).show();
				}
			}
		});
		
		mGetCurrentPostion.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View v) {
				try{
					if (receiver == null )
						receiver = new UpdateReceiver();
					if (filter == null){
						filter = new IntentFilter();
				        filter.addAction("sendgps");
					}
			        
			        ChangsuoPositionActivity.this.registerReceiver(receiver, filter);
			        Intent intent = new Intent(ChangsuoPositionActivity.this,GPSService.class);  
			        startService(intent); 
			        Toast.makeText(ChangsuoPositionActivity.this, "正在获取GPS信息", Toast.LENGTH_SHORT).show();
				} catch(Exception e){
					Toast.makeText(ChangsuoPositionActivity.this, "无法获取GPS信息", Toast.LENGTH_SHORT).show();
				}
			}
		});
		
		mShowCSPostion.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if (mChangsuoInfo.getLon() != 0 && mChangsuoInfo.getLat() != 0) {
					Intent lIntent = new Intent();
					lIntent.putExtra("selectedcs", mChangsuoInfo);
					lIntent.setClass(ChangsuoPositionActivity.this,
							SearchInMapActivity.class);
					ChangsuoPositionActivity.this
							.startActivity(lIntent);
				} else 
					Toast.makeText(ChangsuoPositionActivity.this, "没有该场所的坐标信息", Toast.LENGTH_LONG).show();
			}
		});
	}
	
	public class UpdateReceiver extends BroadcastReceiver{
	    
        @Override
        public void onReceive(Context context, Intent intent) {
        	GeoLatLng strGpsValue = (GeoLatLng)intent.getExtras().get("geolatlon");
        	l_lat = strGpsValue.latitude;
        	l_lon = strGpsValue.longitude;
        	mCurrentLon.setText(String.valueOf(l_lon));
			mCurrentLat.setText(String.valueOf(l_lat));
        }
	}
}
